//
//  EmoticonToolBar.swift
//  自定义表情键盘
//
//  Created by YYY on 2017/9/5.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

//设置代理

protocol EmoticonToolBarDelegate: NSObjectProtocol {
    //协议方法
    func emoticonToolBarDidSelectedItemIndex(toolBar: EmoticonToolBar, index: Int)
}
class EmoticonToolBar: UIView {

    //定义代理
    weak var delegate: EmoticonToolBarDelegate?
    
    //选中按钮的索引
    var selectedIndex: Int = 0
    {
        didSet
        {
            //取消所有按钮的选中状态
            for btn in subviews as! [UIButton]{
                btn.isSelected = false
            }
            
            //设置当前选中按钮状态
            (subviews[selectedIndex] as! UIButton).isSelected =  true
        }
    }
    
    override func awakeFromNib() {
        
        //初始化布局
        setupUI()
    }

    //刷新布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let count = subviews.count
        let width = bounds.width/CGFloat(count)
        
        let rect = CGRect(x: 0, y: 0, width: width, height: bounds.height)
        
        for (i, btn) in subviews.enumerated()
        {
            btn.frame = rect.offsetBy(dx: CGFloat(i) * width, dy: 0)
        }
    }
    
    // MARK: - 点击分组按钮
    @objc fileprivate func clickItem(btn: UIButton)
    {
        //通知代理执行协议方法
        delegate?.emoticonToolBarDidSelectedItemIndex(toolBar: self, index: btn.tag)
    }
}

private extension EmoticonToolBar
{
    func setupUI()
    {
        //初始化布局
        let manager = EmotionManager.shared
        
        for (i,model) in manager.emotionsGroupArray.enumerated()
        {
            //按钮实例化
            let btn = UIButton()
            
            //compose_emotion_table_left_normal@2x
        
            //compose_emotion_table_left_selected@2x
            //设置按钮属性
            let imageNameNormal = "compose_emotion_table_\(model.bgImageName ?? "")_normal"
            let imageNameSelected = "compose_emotion_table_\(model.bgImageName ?? "")_selected"
            
            var imageNormal = UIImage(named: imageNameNormal, in: manager.bundle, compatibleWith: nil)
            var imageSelected = UIImage(named: imageNameSelected, in: manager.bundle, compatibleWith: nil)
            
            //拉伸图片
            let size = imageNormal?.size ?? CGSize()
            let inset = UIEdgeInsetsMake(size.height * 0.5, size.width * 0.5, size.height * 0.5, size.width * 0.5)
            
            imageNormal = imageNormal?.resizableImage(withCapInsets: inset)
            imageSelected = imageSelected?.resizableImage(withCapInsets: inset)
            
            //设置背景图片
            btn.setBackgroundImage(imageNormal, for: .normal)
            btn.setBackgroundImage(imageSelected, for: .selected)
            btn.setBackgroundImage(imageSelected, for: .highlighted)
            
            btn.setTitle(model.groupName, for: .normal)
            btn.setTitleColor(UIColor.white, for: .normal)
            btn.setTitleColor(UIColor.darkGray, for: .highlighted)
            btn.setTitleColor(UIColor.darkGray, for: .selected)
            btn.sizeToFit()
            
            //添加按钮
            addSubview(btn)
            
            //设置按钮tag值
            btn.tag = i
            
            //添加监听方法
            btn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
        }
        //默认选中第一组按钮
        (subviews[0] as! UIButton).isSelected = true
    }
}
